﻿using System.Collections.Generic;
using CfgMusic;
using com.m3sdk.common;
using CSUtils;
using G02.SceneConfig;
using HitBall.UI.SuperSetting;
using m3sdk;
using TripleJump;
using U3dGame;
using U3dGameUI;
using U3dUtils;
using UI00;
using UnityEngine;
using UnityEngine.UI;

namespace G02.TripleJump.UI {
    public class UI_Setup : UIBase {
        public static string prefabPath;

        protected override string _GetUIPrefabPath() {
            if (prefabPath != null) {
                return prefabPath;
            }
            return "TripleJump/UI/UI_Setup/UI_Setup.prefab";
        }

        public static Transform parent;

        protected override Transform _GetUIParent() {
            if (parent != null) {
                return parent;
            }
            return UIBase.uiLayer3_ui;
        }

        CToggle shakeButton, musicButton;

        protected override void _Init() {
            shakeButton = GetControl<CToggle>("view/shakeButton");
            musicButton = GetControl<CToggle>("view/musicButton");

            GetControl<Button>("view/clearButton").
                onClick.AddListener(delegate() {
                    Close();
                });

            UI_LanguageSetting.languageDic = new Dictionary<int, string>() {
                { 6, "中文简体(ChineseSimplified)" }, { 41, "中文繁体(ChineseTraditional)" }, { 10, "English(English)" }, //英语
            };

            shakeButton.onSelectEvent += delegate(CToggle toggle) {
                CMusicManage.PlayMusic(MusicCfg.button_negative);
                TripleJumpData.curr.mobileVibrate = toggle.isSelected;
                if (openFrame != Time.frameCount) {
                    if (toggle.isSelected) {
                        CVibrator.OpenVibrator(0, 400);
                    } else {
                        CDelayMain.Delay(0.4f).SetPauseEnd("").onCallback = delegate {
                            CVibrator.OpenVibrator(0, 400);
                        };
                    }
                }
                CVibrator.isUse = toggle.isSelected;
            };
            musicButton.onSelectEvent += delegate(CToggle toggle) {
                int value = toggle.isSelected ? 1 : 0;
                TripleJumpData.curr.mobileMusic = toggle.isSelected;
                if (toggle.isSelected) {
                    AudioListener.volume = value;
                } else {
                    CDelayMain.Delay(0.4f).SetPauseEnd("").onCallback = delegate {
                        AudioListener.volume = value;
                    };
                }
                CMusicManage.PlayMusic(MusicCfg.button_negative);
            };
        }

        public static void S_GameInit() {
            AudioListener.volume = TripleJumpData.curr.mobileMusic ? 1 : 0;
            CVibrator.isUse = TripleJumpData.curr.mobileVibrate;
        }

        int openFrame;

        protected override void _Open(Dictionary<string, object> paraDic) {
            openFrame = Time.frameCount;
            CMonoMonitor.UpdateEventAdd("UI_SuperSetting", 0, "", UI_SuperSetting.S_Update);
            CMusicManage.PlayMusic(MusicCfg.button_positive);
            shakeButton.S_Select(TripleJumpData.curr.mobileVibrate);
            musicButton.S_Select(TripleJumpData.curr.mobileMusic);
        }

        protected override void _Close() {
            CMusicManage.PlayMusic(MusicCfg.button_negative);

            //CMonoMonitor.UpdateEventRemove(UI_SuperSetting.S_Update);
            CMonoMonitor.UpdateEventRemove("UI_SuperSetting");
        }
    }
}
